package com.xinxing.city.recovery.order;


import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.xinxing.city.recovery.DI;
import com.xinxing.city.recovery.interfaces.IDeviceKernel;
import com.xinxing.city.recovery.interfaces.IDoorState;
import com.xinxing.city.recovery.interfaces.IOrderManager;
import com.xinxing.city.recovery.interfaces.IOverFlowListener;
import com.xinxing.city.recovery.interfaces.ISerialSendReceiveData;
import com.xinxing.city.recovery.serialport.State;


public class OrderManager implements IOrderManager {
    private IDeviceKernel deviceKernel;
    private ISerialSendReceiveData serial;

    private long originalWeight;
    private int roomNumber;
    private String orderCode;
    private byte doorStart;
    private boolean isRun = false;

    /**
     * 选择对应物品柜进行交投
     * @param num       需要操作的门
     * @param orderCode 订单号
     */
    @Override
    public void start(int num, String orderCode) {
        deviceKernel = DI.getInstance(IDeviceKernel.class);
        serial = deviceKernel.getSerial();
        isRun = true;
        roomNumber = num;
        this.orderCode = orderCode;
        serial.getWeight(roomNumber, (num1, weight) -> {
//            Log.e("OrderManager", "第一次重量：" + weight);
            serial.openDoor(num1);
            originalWeight = weight;
            handler.postDelayed(runnable, 50);
        });
        serial.setDoorState(new IDoorState() {
            @Override
            public void doorState(int num, byte state) {
//                Log.e("OrderManager","状态 "+state);
                doorStart = state;
            }
        });

    }
    private Thread orderThread = new Thread(){
        @Override
        public void run() {
            super.run();
            while (isRun){
                try {
                    sleep(50);
                    if(doorStart == com.xinxing.city.recovery.serialport.State.Room.CLOSE_DOOR){
                        if (deviceKernel.getRuntime().isCut()) {
                            serial.closeCut(roomNumber);
                        }
                    }else if(doorStart == com.xinxing.city.recovery.serialport.State.Room.CLOSE_CUT){
                        serial.overflow(new IOverFlowListener() {
                            @Override
                            public void overFlowState(boolean num1, boolean num2, boolean num3, boolean num4) {

                            }
                        });
                        //满溢
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    };

    /**
     * 取消交投
     * 执行关门
     * 取消订单
     */
    @Override
    public void cancel() {
        cancelOrder();
        end();
    }

    /**
     * 取消交投 取消订单
     */

    public void cancelOrder() {

    }

    /**
     * 获取称重信息
     * @param num
     * @param weight
     */
    @Override
    public void weight(int num, long weight) {

        if (weight > (originalWeight + 50)) {
            float nowWeight = (float) ((weight - originalWeight) / 1000);
            Log.e("OrderManager", "nowWeight " + nowWeight);
            //重量有变化，显示
            //停止称重
            handler.removeCallbacks(runnable);
        }

    }

    /**
     * 正确执行交投后，倒计时结束，或点击完成
     * 执行关门
     * 上报订单信息
     */
    @Override
    public void finish() {
        upData();
        end();
    }

    /**
     * 上报数据接口
     */
    @Override
    public void upData() {


    }

    @Override
    public void stop() {
        isRun = false;
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Log.e("OrderManager", "再次称重");

            serial.getWeight(roomNumber, (num, weight) -> {
                Log.e("OrderManager", "第二次重量为：" + weight);
                weight(num, weight);
                handler.postDelayed(this, 50);
            });

        }
    };

    /**
     * 停止称重
     * 执行关门
     */
    private void end() {

        handler.sendEmptyMessageDelayed(1,50);

    }
    private Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    serial.closeDoor(roomNumber);
                    Log.e("OrderManager", "关门");
                    break;
            }
        }
    };
}
